using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectChuongTrinhKhuyenMai()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_CHUONGTRINHKHUYENMAI
			};
			return View(CreateViewModel(data));
		}

		public ActionResult SelectChuongTrinhKhuyenMaiPaging(int page = 1)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_CHUONGTRINHKHUYENMAI,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListChuongTrinhKhuyenMai", CreateViewModel(data));
		}


		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeleteChuongTrinhKhuyenMai(int id)
		{
			return Json(new{
				Success = _repChuongTrinhKhuyenMai.Delete(id),
				RecordCount = _repChuongTrinhKhuyenMai.SelectAll().Count,
				DeleteId = id
			});
		}

		public ActionResult DetailOfChuongTrinhKhuyenMai(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_CHUONGTRINHKHUYENMAI,
				IntID = id
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateChuongTrinhKhuyenMai(FormCollection forms)
		{
            string id = forms.GetValues("ChuongTrinhKhuyenMai_ID")[0];
            string tenchuongtrinhkhuyenmai = forms.GetValues("ChuongTrinhKhuyenMai_TenChuongTrinhKhuyenMai")[0];
            string noidung = forms.GetValues("ChuongTrinhKhuyenMai_NoiDung")[0];
            var file = Request.Files["ChuongTrinhKhuyenMai_HinhMinhHoa"];
            string refName = "";
            var editItem = _repChuongTrinhKhuyenMai.SelectByID(int.Parse(id));
            editItem.TenChuongTrinhKhuyenMai = tenchuongtrinhkhuyenmai;
            editItem.NoiDung = noidung;
            var result = false;
            if (file.ContentLength != 0)
            {
                result = FileHelper.UploadFile(file, Server.MapPath("/Content/Images/Items/"), id, editItem.HinhMinhHoa, ref refName);
                editItem.HinhMinhHoa = refName;
            }
            result = _repChuongTrinhKhuyenMai.Save();
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_CHUONGTRINHKHUYENMAI,
                IntID = int.Parse(id.Replace(".", "").Replace(",", "")),
                InfoText = result ? "Item has been updated" : "Cannot update this item"
            };
            return View("DetailOfChuongTrinhKhuyenMai", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertChuongTrinhKhuyenMai(FormCollection forms)
		{
            string tenchuongtrinhkhuyenmai = forms.GetValues("ChuongTrinhKhuyenMai_TenChuongTrinhKhuyenMai")[0];
            string noidung = forms.GetValues("ChuongTrinhKhuyenMai_NoiDung")[0];
            var file = Request.Files["ChuongTrinhKhuyenMai_HinhMinhHoa"];
            string refName = "";
            var newItem = new ChuongTrinhKhuyenMai
            {
                TenChuongTrinhKhuyenMai = tenchuongtrinhkhuyenmai,
                NoiDung = noidung,
                HinhMinhHoa = "735_101.swf"
            };
            var result = _repChuongTrinhKhuyenMai.Insert(newItem);
            if (result)
            {
                if (file.ContentLength != 0)
                {
                    result = FileHelper.UploadFile(file, Server.MapPath("/Content/Images/Items/"), newItem.ID.ToString(), string.Empty, ref refName);
                    if (result)
                    {
                        var addedItem = _repChuongTrinhKhuyenMai.SelectByID(newItem.ID);
                        addedItem.HinhMinhHoa = refName;
                        _repChuongTrinhKhuyenMai.Save();
                    }
                }
            }
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_CHUONGTRINHKHUYENMAI,
                InfoText = result ? "New item has been added" : "Cannot insert new item"
            };
            return View("SelectChuongTrinhKhuyenMai", CreateViewModel(data));
		}
	}
}